// 首页
// import { isEmpty } from 'lodash'
import * as types from '@/store/mutation-types'
import api from '@/tools/api.js'
import promise from '@/tools/promise'
import storage from '@/tools/storage'
import { SUCCESS_STATUS } from '@/config/status'
// info: 登录的该用户信息
// detail: 后台进行查看的具体的某一条用户信息
const state = {
  info: {},
  detail: {},
  list: [],
  userIncreTendency: {},
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  }
}

// mutations
const mutations = {
  [types.USER_LOGIN] (state, payload) {
    state.info = payload
    storage.set(types.USER_INFO, payload)
  },
  [types.CLEAR_USER_INFO] (state) {
    state.info = {}
    storage.remove(types.USER_INFO)
  },
  [types.SET_USER_INFO_FROM_LOCAL] (state) {
    state.info = storage.get(types.USER_INFO)
  },
  [types.EDIT_SELF_INFO] (state, payload) {
    state.info.nickname = payload.nickname
    state.info.sex = payload.sex
  },
  [types.USER_INCRE_TENDENCY] (state, payload) {
    state.userIncreTendency = payload
  },
  [types.GET_USER_LIST] (state, payload) {
    state.list = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.GET_USER_DETAIL] (state, payload) {
    state.detail = payload
  }
}

// actions
const actions = {
  // 用户登录
  async login ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在登录...'}
    let loginRes = await api('serveruser/user/User/login', payload, config)
    if (SUCCESS_STATUS === loginRes.status) {
      commit(types.USER_LOGIN, loginRes.res)
    }
    return promise(loginRes)
  },
  // 清除用户信息
  clearUserInfo ({ commit }) {
    commit(types.CLEAR_USER_INFO)
  },
  // 从某地sessionStorage取用户信息至vuex当中
  setUserInfoFromLocal ({ commit }) {
    commit(types.SET_USER_INFO_FROM_LOCAL)
  },
  // 用户退出登录
  async logout ({ commit }) {
    let config = {loading: true, loadingText: '正在安全退出...'}
    let logoutRes = await api('serveruser/user/User/logout', {}, config)
    return promise(logoutRes)
  },
  // 用户编辑自己的信息
  async editSelfInfo ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在修改信息...'}
    let editRes = await api('serveruser/user/User/editSelfInfo', payload, config)
    if (SUCCESS_STATUS === editRes.status) {
      commit(types.EDIT_SELF_INFO, payload)
    }
    return promise(editRes)
  },
  // 用户修改自己的登录密码
  async editSelfLoginPassword ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在修改密码...'}
    let editRes = await api('serveruser/user/User/editSelfLoginPassword', payload, config)
    return promise(editRes)
  },
  // 获取最近一周新注册用户的统计
  async userIncreTendency ({ commit }) {
    let getRes = await api('serveruser/user/User/userIncreTendency')
    SUCCESS_STATUS === getRes.status && commit(types.USER_INCRE_TENDENCY, getRes.res)
    return promise(getRes)
  },
  // 后台获取用户列表
  async getList ({ commit }, payload) {
    payload = {list_rows: 20, ...payload}
    let config = {loading: true, loadingText: '正在获取用户列表'}
    let getRes = await api('serveruser/user/User/list', payload, config)
    commit(types.GET_USER_LIST, getRes.res)
  },
  // 后台获取某用户的详情信息
  async userInfo ({ commit }, payload) {
    let getRes = await api('serveruser/user/User/info', payload)
    SUCCESS_STATUS === getRes.status && commit(types.GET_USER_DETAIL, getRes.res)
  },
  // 后台重置用户登录密码
  async resetUserLoginPwd ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在重置用户登录密码'}
    let resetRes = await api('serveruser/user/User/resetUserLoginPassword', payload, config)
    return promise(resetRes)
  },
  // 后台重置用户支付密码
  async resetUserPayPwd ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在重置用户支付密码'}
    let resetRes = await api('serveruser/user/User/resetUserPayPassword', payload, config)
    return promise(resetRes)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
